package org.linlinjava.litemall.admin.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.dto.AdminLumiereFormulaSpecificationsDataProportioningDTO;
import org.linlinjava.litemall.admin.vo.formulamaterial.FormulaSpecificationsDataProportioningReqVO;
import org.linlinjava.litemall.core.exception.util.ServiceExceptionUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;
import java.util.stream.Collectors;

import static org.linlinjava.litemall.admin.errorcode.ErrorCodeConstants.LUMIERE_FORMULA_FORMULA_SPECIFICATIONS_DATA_PROPORTIONING_IS_USE;

/**
 * @auth :ply
 * @date 2024/5/17 14:07
 * @Description TODO
 * 管理配方规格数据配比管理服务
 */
@Service
@RequiredArgsConstructor
@Transactional
public class AdminLumiereFormulaSpecificationsDataProportioningService {

    private final LumiereFormulaSpecificationsDataProportioningService lumiereFormulaSpecificationsDataProportioningService;

    private final LumiereFormulaMaterialService lumiereFormulaMaterialService;

    private final LumiereFormulaSpecificationsService lumiereFormulaSpecificationsService;

    private final LumiereMaterialClassificationService lumiereMaterialClassificationService;


    /**
     * 根据查询条件查询配方规格数据配比列表
     * @param lumiereFormulaSpecificationsDataProportioning 查询条件
     * @param page 当前页数，默认为1
     * @param limit 每页数量，默认为10
     * @return 配方规格数据配比列表
     */
    public ResponseUtil.Response listLumiereFormulaSpecificationsDataProportioning(LumiereFormulaSpecificationsDataProportioning lumiereFormulaSpecificationsDataProportioning,
                                                                                   @RequestParam(defaultValue = "1") Integer page,
                                                                                   @RequestParam(defaultValue = "10") Integer limit) {
        // 构建查询条件
        LambdaQueryWrapper<LumiereFormulaSpecificationsDataProportioning> queryWrapper = new LambdaQueryWrapper<>();
        Optional.ofNullable(lumiereFormulaSpecificationsDataProportioning.getFormulaSpecificationsId())
                .filter(id -> id != 0) // 判断formulaTypeId是否不等于0
                .ifPresent(id -> queryWrapper.eq(LumiereFormulaSpecificationsDataProportioning::getFormulaSpecificationsId, id));

        // 执行查询
        IPage<LumiereFormulaSpecificationsDataProportioning> lumiereFormulaList = lumiereFormulaSpecificationsDataProportioningService.page(new Page<>(page,limit),queryWrapper);

        List proportioningDTOList = lumiereFormulaList.getRecords().stream().map(proportioning -> {
            AdminLumiereFormulaSpecificationsDataProportioningDTO dto = new AdminLumiereFormulaSpecificationsDataProportioningDTO();
            List<LumiereFormulaMaterial> materials = lumiereFormulaMaterialService.list(new LambdaQueryWrapper<LumiereFormulaMaterial>().eq(LumiereFormulaMaterial::getId, proportioning.getFormulaMaterialId()));
            BeanUtils.copyProperties(proportioning, dto);
            materials.forEach(dto::setLumiereFormulaMaterial);
            return dto;
        }).collect(Collectors.toList());
        lumiereFormulaList.setRecords(proportioningDTOList);
        // 返回结果
        return ResponseUtil.okPage(lumiereFormulaList);
    }

    /**
     * 根据查询条件查询配方规格数据配比列表
     * @param lumiereFormulaSpecificationsDataProportioning 查询条件
     * @return 配方规格数据配比列表
     */
    public ResponseUtil.Response newListLumiereFormulaSpecificationsDataProportioning(LumiereFormulaSpecificationsDataProportioning lumiereFormulaSpecificationsDataProportioning) {
        //查询到规格的原料
        /*List<LumiereFormulaSpecificationsDataProportioning> lumiereFormulaSpecificationsDataProportionings = lumiereFormulaSpecificationsDataProportioningService.list(Wrappers.lambdaQuery(LumiereFormulaSpecificationsDataProportioning.class)
                .eq(LumiereFormulaSpecificationsDataProportioning::getFormulaSpecificationsId, lumiereFormulaSpecificationsDataProportioning.getFormulaSpecificationsId())
                .orderByDesc(LumiereFormulaSpecificationsDataProportioning::getGroupName)
                .orderByAsc(LumiereFormulaSpecificationsDataProportioning::getAddTime));*/

        List<LumiereFormulaSpecificationsDataProportioning> lumiereFormulaSpecificationsDataProportionings = lumiereFormulaSpecificationsDataProportioningService.selectJoinList(LumiereFormulaSpecificationsDataProportioning.class,new MPJLambdaWrapper<LumiereFormulaSpecificationsDataProportioning>()
                .selectAll(LumiereFormulaSpecificationsDataProportioning.class)
                .leftJoin(LumiereFormulaMaterial.class,LumiereFormulaMaterial::getId,LumiereFormulaSpecificationsDataProportioning::getFormulaMaterialId)
                .eq(LumiereFormulaSpecificationsDataProportioning::getFormulaSpecificationsId, lumiereFormulaSpecificationsDataProportioning.getFormulaSpecificationsId())
                .orderByAsc(LumiereFormulaSpecificationsDataProportioning::getMaterialType)
                .orderByDesc(LumiereFormulaSpecificationsDataProportioning::getGroupName)
        );


        List<AdminLumiereFormulaSpecificationsDataProportioningDTO> proportioningDTOList = lumiereFormulaSpecificationsDataProportionings.stream() // 将查询结果转换为 Stream
                .map(formulaSpecificationsDataProportioning -> { // 遍历每个原料数据
                    // 创建 DTO 对象
                    AdminLumiereFormulaSpecificationsDataProportioningDTO formulaSpecificationsDataProportioningDTO = new AdminLumiereFormulaSpecificationsDataProportioningDTO();
                    // 设置配方原料数据
                    LumiereFormulaMaterial formulaMaterial = lumiereFormulaMaterialService.getById(formulaSpecificationsDataProportioning.getFormulaMaterialId());
                    formulaSpecificationsDataProportioningDTO.setLumiereFormulaMaterial(formulaMaterial);
                    BeanUtils.copyProperties(formulaSpecificationsDataProportioning, formulaSpecificationsDataProportioningDTO);

                    //组名
                    /*LumiereMaterialClassification byId = lumiereMaterialClassificationService.getById(formulaSpecificationsDataProportioningDTO.getGroupName());
                    formulaSpecificationsDataProportioningDTO.setGroupMaterialName(byId.getClassificationName());
                    formulaSpecificationsDataProportioningDTO.setGroupMaterialColor(byId.getClassificationColour());*/
                    return formulaSpecificationsDataProportioningDTO; // 返回 DTO 对象
                }).collect(Collectors.toList()); // 将结果收集为 List

        //Map<Integer, List<AdminLumiereFormulaSpecificationsDataProportioningDTO>> integerListMap = CollStreamUtil.groupByKey(proportioningDTOList, AdminLumiereFormulaSpecificationsDataProportioningDTO::getGroupName);
        return ResponseUtil.ok(proportioningDTOList);
        //查询到规格的数据
        //LumiereFormulaSpecifications specifications = lumiereFormulaSpecificationsService.getById(lumiereFormulaSpecificationsDataProportioning.getFormulaSpecificationsId());

        //通过规格查询到原料数据
        //List<LumiereFormulaMaterial> formulaMaterialList = lumiereFormulaMaterialService.list(new LambdaQueryWrapper<LumiereFormulaMaterial>().eq(LumiereFormulaMaterial::getFormulaId, specifications.getFormulaId()));

        // 使用 Stream API 查询并处理数据
        /*List<AdminLumiereFormulaSpecificationsDataProportioningDTO> proportioningDTOList = lumiereFormulaMaterialService.list(
                        // 构造查询条件
                        new LambdaQueryWrapper<LumiereFormulaMaterial>()
                                .eq(LumiereFormulaMaterial::getFormulaId, specifications.getFormulaId())
                                .orderByAsc(LumiereFormulaMaterial::getMaterialType)
                ).stream() // 将查询结果转换为 Stream
                .map(lumiereFormulaMaterial -> { // 遍历每个原料数据
                    // 创建 DTO 对象
                    AdminLumiereFormulaSpecificationsDataProportioningDTO formulaSpecificationsDataProportioningDTO = new AdminLumiereFormulaSpecificationsDataProportioningDTO();
                    // 设置配方原料数据
                    formulaSpecificationsDataProportioningDTO.setLumiereFormulaMaterial(lumiereFormulaMaterial);

                    // 查询配方原料配比数据
                    LumiereFormulaSpecificationsDataProportioning proportioning = lumiereFormulaSpecificationsDataProportioningService.getOne(
                            new LambdaQueryWrapper<LumiereFormulaSpecificationsDataProportioning>()
                                    .eq(LumiereFormulaSpecificationsDataProportioning::getFormulaMaterialId, lumiereFormulaMaterial.getId())
                                    .eq(LumiereFormulaSpecificationsDataProportioning::getFormulaSpecificationsId,lumiereFormulaSpecificationsDataProportioning.getFormulaSpecificationsId())
                    );

                    // 如果查询结果不为空，则复制属性
                    if (proportioning != null) {
                        BeanUtils.copyProperties(proportioning, formulaSpecificationsDataProportioningDTO);
                    }

                    return formulaSpecificationsDataProportioningDTO; // 返回 DTO 对象
                }).collect(Collectors.toList()); // 将结果收集为 List*/

        //返回结果
        //return ResponseUtil.okPageInfo(proportioningDTOList);
    }

    /**
     * 根据指定ID获取配方规格数据配比详情。
     * @param id 配方ID
     * @return 包含配方规格数据配比详情的响应结果
     */
    public ResponseUtil.Response details(Integer id){
        return ResponseUtil.ok(lumiereFormulaSpecificationsDataProportioningService.getById(id));
    }

    /**
     * 添加新的配方规格数据配比。
     * @param lumiereFormulaSpecificationsDataProportioning 待添加的配方规格数据配比对象
     * @return 添加操作的响应结果
     */
    /*public ResponseUtil.Response addLumiereFormulaSpecificationsDataProportioning(FormulaSpecificationsDataProportioningReqVO lumiereFormulaSpecificationsDataProportioning) {
        lumiereFormulaSpecificationsDataProportioning.setId(null);
        List<LumiereFormulaSpecificationsDataProportioning> list = lumiereFormulaSpecificationsDataProportioningService.list(Wrappers.lambdaQuery(LumiereFormulaSpecificationsDataProportioning.class)
                .eq(LumiereFormulaSpecificationsDataProportioning::getFormulaSpecificationsId, lumiereFormulaSpecificationsDataProportioning.getFormulaSpecificationsId())
                .eq(LumiereFormulaSpecificationsDataProportioning::getFormulaMaterialId, lumiereFormulaSpecificationsDataProportioning.getFormulaMaterialId()));
        if(list.size()>0){
            throw ServiceExceptionUtil.exception(LUMIERE_FORMULA_FORMULA_SPECIFICATIONS_DATA_PROPORTIONING_IS_USE);
        }


        //默认自己一组
        if(lumiereFormulaSpecificationsDataProportioning.getGroupName()==null){
            //LumiereFormulaMaterial formulaMaterial = lumiereFormulaMaterialService.getById(lumiereFormulaSpecificationsDataProportioning.getFormulaMaterialId());

            //颜色
            lumiereFormulaSpecificationsDataProportioning.setIsDefault(true);
            lumiereFormulaSpecificationsDataProportioning.setGroupColor(getColor());
            //lumiereFormulaSpecificationsDataProportioning.setGroupName(formulaMaterial.getMaterialClassificationId());
            lumiereFormulaSpecificationsDataProportioningService.save(lumiereFormulaSpecificationsDataProportioning);

            lumiereFormulaSpecificationsDataProportioning.setGroupName(lumiereFormulaSpecificationsDataProportioning.getId());
            lumiereFormulaSpecificationsDataProportioningService.updateById(lumiereFormulaSpecificationsDataProportioning);
        }else{
            //获取选择组的 组号
            LumiereFormulaSpecificationsDataProportioning proportioningServiceById = lumiereFormulaSpecificationsDataProportioningService.getById(lumiereFormulaSpecificationsDataProportioning.getGroupName());
            lumiereFormulaSpecificationsDataProportioning.setGroupName(proportioningServiceById.getGroupName());
            //颜色
            lumiereFormulaSpecificationsDataProportioning.setGroupColor(proportioningServiceById.getGroupColor());
            //是否原配方
            if(lumiereFormulaSpecificationsDataProportioning.getIsDefault()!=null&&lumiereFormulaSpecificationsDataProportioning.getIsDefault()){
                //更改组里其它默认设置
                lumiereFormulaSpecificationsDataProportioningService.update(Wrappers.lambdaUpdate(LumiereFormulaSpecificationsDataProportioning.class)
                        .eq(LumiereFormulaSpecificationsDataProportioning::getFormulaSpecificationsId,lumiereFormulaSpecificationsDataProportioning.getFormulaSpecificationsId())
                        .eq(LumiereFormulaSpecificationsDataProportioning::getGroupName,lumiereFormulaSpecificationsDataProportioning.getGroupName())
                        .set(LumiereFormulaSpecificationsDataProportioning::getIsDefault,false));
            }
            lumiereFormulaSpecificationsDataProportioningService.save(lumiereFormulaSpecificationsDataProportioning);
        }


        if(!lumiereFormulaSpecificationsDataProportioning.getSync()){return ResponseUtil.ok();}
        // 批量同步
        LumiereFormulaSpecifications formulaSpecifications = lumiereFormulaSpecificationsService.getById(lumiereFormulaSpecificationsDataProportioning.getFormulaSpecificationsId());
        List<LumiereFormulaSpecifications> formulaSpecificationsList = lumiereFormulaSpecificationsService.list(Wrappers.lambdaQuery(LumiereFormulaSpecifications.class)
                .ne(LumiereFormulaSpecifications::getId, formulaSpecifications.getId())
                .eq(LumiereFormulaSpecifications::getFormulaId, formulaSpecifications.getFormulaId()));

        for (LumiereFormulaSpecifications formulaSpecification : formulaSpecificationsList) {
            if (formulaSpecification.getId().intValue() == lumiereFormulaSpecificationsDataProportioning.getFormulaSpecificationsId()) continue;

            // 如果当前项是默认的，将其他项的isDefault设置为false
            if (lumiereFormulaSpecificationsDataProportioning.getIsDefault()) {
                // 设置其他项为非默认
                lumiereFormulaSpecificationsDataProportioning.setIsDefault(false);
            }

            // 设置新的ID并保存
            lumiereFormulaSpecificationsDataProportioning.setId(null);
            lumiereFormulaSpecificationsDataProportioning.setFormulaSpecificationsId(formulaSpecification.getId());
            lumiereFormulaSpecificationsDataProportioningService.save(lumiereFormulaSpecificationsDataProportioning);
        }
        return ResponseUtil.ok();
    }*/

    public ResponseUtil.Response addLumiereFormulaSpecificationsDataProportioning(FormulaSpecificationsDataProportioningReqVO lumiereFormulaSpecificationsDataProportioning) {

       /* List<LumiereFormulaSpecificationsDataProportioning> list = lumiereFormulaSpecificationsDataProportioningService.list(Wrappers.lambdaQuery(LumiereFormulaSpecificationsDataProportioning.class)
                .eq(LumiereFormulaSpecificationsDataProportioning::getFormulaSpecificationsId, lumiereFormulaSpecificationsDataProportioning.getFormulaSpecificationsId())
                .eq(LumiereFormulaSpecificationsDataProportioning::getFormulaMaterialId, lumiereFormulaSpecificationsDataProportioning.getFormulaMaterialIds().get(0)));
        if (list.size() > 0) {
            throw ServiceExceptionUtil.exception(LUMIERE_FORMULA_FORMULA_SPECIFICATIONS_DATA_PROPORTIONING_IS_USE);
        }*/

        // 颜色
        String color = getColor();
        Integer groupName = null;

        for (int i = 0; i < lumiereFormulaSpecificationsDataProportioning.getFormulaMaterialIds().size(); i++) {
            LumiereFormulaSpecificationsDataProportioning saveFormulaSpecificationsDataProportioning = new LumiereFormulaSpecificationsDataProportioning();
            saveFormulaSpecificationsDataProportioning.setMaterialId(lumiereFormulaSpecificationsDataProportioning.getMaterialId());
            saveFormulaSpecificationsDataProportioning.setMaterialClassificationId(lumiereFormulaSpecificationsDataProportioning.getMaterialClassificationId());
            saveFormulaSpecificationsDataProportioning.setFormulaSpecificationsId(lumiereFormulaSpecificationsDataProportioning.getFormulaSpecificationsId());
            saveFormulaSpecificationsDataProportioning.setFormulaMaterialId(lumiereFormulaSpecificationsDataProportioning.getFormulaMaterialIds().get(i));
            saveFormulaSpecificationsDataProportioning.setMaterialType(lumiereFormulaSpecificationsDataProportioning.getMaterialType());
            saveFormulaSpecificationsDataProportioning.setSurcharge(lumiereFormulaSpecificationsDataProportioning.getSurcharge());
            saveFormulaSpecificationsDataProportioning.setSpecificationsDose(lumiereFormulaSpecificationsDataProportioning.getSpecificationsDose());
            saveFormulaSpecificationsDataProportioning.setPrice(lumiereFormulaSpecificationsDataProportioning.getPrice());
            saveFormulaSpecificationsDataProportioning.setQuantity(lumiereFormulaSpecificationsDataProportioning.getQuantity());
            saveFormulaSpecificationsDataProportioning.setChargingCondition(lumiereFormulaSpecificationsDataProportioning.getChargingCondition());
            saveFormulaSpecificationsDataProportioning.setQuantityRestrict(lumiereFormulaSpecificationsDataProportioning.getQuantityRestrict());
            saveFormulaSpecificationsDataProportioning.setUnit(lumiereFormulaSpecificationsDataProportioning.getUnits().get(i));
            saveFormulaSpecificationsDataProportioning.setConsumeSystem(lumiereFormulaSpecificationsDataProportioning.getConsumeSystem());
            saveFormulaSpecificationsDataProportioning.setChargingMode(lumiereFormulaSpecificationsDataProportioning.getChargingMode());
            saveFormulaSpecificationsDataProportioning.setIsDisplay(lumiereFormulaSpecificationsDataProportioning.getIsDisplay());
            saveFormulaSpecificationsDataProportioning.setIsEdit(false);
            saveFormulaSpecificationsDataProportioning.setMakeUnit(StrUtil.isEmpty(lumiereFormulaSpecificationsDataProportioning.getMakeUnit())?" ":lumiereFormulaSpecificationsDataProportioning.getMakeUnit());

            // 各为一组
            if (lumiereFormulaSpecificationsDataProportioning.getGroupName() == null && lumiereFormulaSpecificationsDataProportioning.getLoneliness()) {
                saveFormulaSpecificationsDataProportioning.setIsDefault(i == 0);
                saveFormulaSpecificationsDataProportioning.setGroupColor(getColor());
                saveFormulaSpecificationsDataProportioning.setIsDefault(true);
                lumiereFormulaSpecificationsDataProportioningService.save(saveFormulaSpecificationsDataProportioning);
                saveFormulaSpecificationsDataProportioning.setGroupName(saveFormulaSpecificationsDataProportioning.getId());
                lumiereFormulaSpecificationsDataProportioningService.updateById(saveFormulaSpecificationsDataProportioning);
                continue;
            }

            // 跟随第一个为一组
            if (lumiereFormulaSpecificationsDataProportioning.getGroupName() == null && !lumiereFormulaSpecificationsDataProportioning.getLoneliness()) {
                if (groupName == null) {
                    saveFormulaSpecificationsDataProportioning.setIsDefault(true);
                    saveFormulaSpecificationsDataProportioning.setGroupColor(color);
                    lumiereFormulaSpecificationsDataProportioningService.save(saveFormulaSpecificationsDataProportioning);
                    groupName = saveFormulaSpecificationsDataProportioning.getId();
                    saveFormulaSpecificationsDataProportioning.setGroupName(groupName);
                    lumiereFormulaSpecificationsDataProportioningService.updateById(saveFormulaSpecificationsDataProportioning);
                } else {
                    saveFormulaSpecificationsDataProportioning.setGroupName(groupName);
                    saveFormulaSpecificationsDataProportioning.setGroupColor(color);
                    lumiereFormulaSpecificationsDataProportioningService.save(saveFormulaSpecificationsDataProportioning);
                }
                continue;
            }

            // 与选择的组为一组
            if (lumiereFormulaSpecificationsDataProportioning.getGroupName() != null) {
                LumiereFormulaSpecificationsDataProportioning proportioningServiceById = lumiereFormulaSpecificationsDataProportioningService.getById(lumiereFormulaSpecificationsDataProportioning.getGroupName());
                saveFormulaSpecificationsDataProportioning.setGroupName(proportioningServiceById.getGroupName());
                saveFormulaSpecificationsDataProportioning.setGroupColor(proportioningServiceById.getGroupColor());
                saveFormulaSpecificationsDataProportioning.setIsDefault(false);
                lumiereFormulaSpecificationsDataProportioningService.save(saveFormulaSpecificationsDataProportioning);
            }
        }

        return ResponseUtil.ok();
    }

    //随机生成颜色代码
    public String getColor(){
        //红色
        String red;
        //绿色
        String green;
        //蓝色
        String blue;
        //生成随机对象
        Random random = new Random();
        //生成红色颜色代码
        red = Integer.toHexString(random.nextInt(256)).toUpperCase();
        //生成绿色颜色代码
        green = Integer.toHexString(random.nextInt(256)).toUpperCase();
        //生成蓝色颜色代码
        blue = Integer.toHexString(random.nextInt(256)).toUpperCase();

        //判断红色代码的位数
        red = red.length()==1 ? "0" + red : red ;
        //判断绿色代码的位数
        green = green.length()==1 ? "0" + green : green ;
        //判断蓝色代码的位数
        blue = blue.length()==1 ? "0" + blue : blue ;
        //生成十六进制颜色值
        String color = "#"+red+green+blue;
        List<LumiereFormulaSpecificationsDataProportioning> list = lumiereFormulaSpecificationsDataProportioningService.list(Wrappers.lambdaQuery(LumiereFormulaSpecificationsDataProportioning.class)
                .eq(LumiereFormulaSpecificationsDataProportioning::getGroupColor, color));
        while (list.size()>0){
            color = getColor();
        }
        return color;
    }

    /**
     * 更新配方规格数据配比信息。
     * @param lumiereFormulaSpecificationsDataProportioning 待更新的配方规格数据配比对象
     * @return 更新操作的响应结果
     */
    public ResponseUtil.Response updateLumiereFormulaSpecificationsDataProportioning(LumiereFormulaSpecificationsDataProportioning lumiereFormulaSpecificationsDataProportioning) {
        //辅料没有默认
        /*LumiereFormulaMaterial formulaMaterial = lumiereFormulaMaterialService.getById(lumiereFormulaSpecificationsDataProportioning.getFormulaMaterialId());

        if(formulaMaterial.getMaterialType() == 1){
            lumiereFormulaSpecificationsDataProportioning.setIsDefault(false);
        }*/

        if(lumiereFormulaSpecificationsDataProportioning.getGroupName()!=null){
            List<LumiereFormulaSpecificationsDataProportioning> formulaSpecificationsDataProportioningList = lumiereFormulaSpecificationsDataProportioningService.list(Wrappers.lambdaQuery(LumiereFormulaSpecificationsDataProportioning.class)
                    .eq(LumiereFormulaSpecificationsDataProportioning::getGroupName, lumiereFormulaSpecificationsDataProportioning.getGroupName()));
            //获取选择组的 组号
            LumiereFormulaSpecificationsDataProportioning proportioningServiceById = formulaSpecificationsDataProportioningList.get(0);
            //lumiereFormulaSpecificationsDataProportioningService.getById(lumiereFormulaSpecificationsDataProportioning.getGroupName());
            lumiereFormulaSpecificationsDataProportioning.setGroupName(proportioningServiceById.getGroupName());
            //颜色
            lumiereFormulaSpecificationsDataProportioning.setGroupColor(proportioningServiceById.getGroupColor());
        }else {
            //LumiereFormulaMaterial formulaMaterial = lumiereFormulaMaterialService.getById(lumiereFormulaSpecificationsDataProportioning.getFormulaMaterialId());

            //颜色
            lumiereFormulaSpecificationsDataProportioning.setIsDefault(true);
            lumiereFormulaSpecificationsDataProportioning.setGroupColor(getColor());
            //lumiereFormulaSpecificationsDataProportioning.setGroupName(formulaMaterial.getMaterialClassificationId());
            lumiereFormulaSpecificationsDataProportioning.setGroupName(lumiereFormulaSpecificationsDataProportioning.getId());
        }

        //是否原配方
        if(lumiereFormulaSpecificationsDataProportioning.getIsDefault()){
            //更改组里其它默认设置
            lumiereFormulaSpecificationsDataProportioningService.update(Wrappers.lambdaUpdate(LumiereFormulaSpecificationsDataProportioning.class)
                    .eq(LumiereFormulaSpecificationsDataProportioning::getFormulaSpecificationsId,lumiereFormulaSpecificationsDataProportioning.getFormulaSpecificationsId())
                    .eq(LumiereFormulaSpecificationsDataProportioning::getGroupName,lumiereFormulaSpecificationsDataProportioning.getGroupName())
                    .set(LumiereFormulaSpecificationsDataProportioning::getIsDefault,false));
        }
        lumiereFormulaSpecificationsDataProportioning.setIsEdit(false);
        return lumiereFormulaSpecificationsDataProportioningService.updateById(lumiereFormulaSpecificationsDataProportioning) ? ResponseUtil.ok() : ResponseUtil.fail();
    }

    /**
     * 根据指定ID删除配方规格数据配比。
     * @param id 待删除的路配方规格数据配比ID
     * @return 删除操作的响应结果
     */
    public ResponseUtil.Response deleteLumiereFormulaSpecificationsDataProportioning(Integer id){
        return lumiereFormulaSpecificationsDataProportioningService.removeById(id) ? ResponseUtil.ok() : ResponseUtil.fail();
    }



}
